package cn.jwelll.bxcstock.ui.carin.preview;
import cn.jwelll.bxcstock.base.BaseActivity;
import cn.jwelll.bxcstock.base.BasePresenter;
import cn.jwelll.bxcstock.net.ApiMiddleHelper;
import cn.jwelll.bxcstock.net.listener.DataCallBackListener;
import cn.jwelll.bxcstock.util.LogUtil;
import cn.jwelll.bxcstock.viewmodel.viewmodel.CarInPreviewModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.reactivex.Flowable;

import java.util.HashMap;
import java.util.Map;

public class CarInPreviewPresenter extends BasePresenter<CarInPreviewContract.View> implements CarInPreviewContract.Presenter {
    BaseActivity activity;
    public CarInPreviewPresenter(BaseActivity activity) {
        this.activity=activity;
    }

    @Override
    public void start() {

    }

    @Override
    public void carScanCode(String id) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("id", id);
        Flowable<JsonObject> flowable = apiStrores.carScanCode(biddingMap);
        ApiMiddleHelper.getInstance().toNetIo2Main(null,flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("carScanCode-onSuccess-" + json);
                try {

                    if (getView() != null) {
                        activity.hideProgress();
                        getView().carScanCodeSuccess(new Gson().fromJson(json, CarInPreviewModel.class));
                    }
                } catch (Exception e) {
                    onFailure("数据解析错误");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("carScanCode-onFailure-" + msg);
                if(activity!=null)
                {
                    activity.hideProgress();
                    activity.showToast(msg);
                }
            }
        });
    }
}
